define(function( require , exports ){
	var $ = require('../libs/jquery/jquery');
	var _ = require('../libs/backbone/underscore');
	var Backbone = require('../libs/backbone/backbone');
	require('../libs/jquery/form/jquery.form')($);
	require('../libs/bootstrap/js/bootstrap')($);
	require('../libs/jquery/placeholder/jquery.html5.placeholder')($);
	
	var json = {
			"11" :{
				"1101" : "北京",
				"1102" : "东城区",
				"1103" : "西城区",
				"1104" : "崇文区",
				"1105" : "朝阳区",
				"1106" : "海淀区",
				"1107" : "丰台区",
				"1108" : "石景山区",
				"1109" : "房山区",
				"1110" : "通州区",
				"1111" : "顺义区",
				"1112" : "大兴区",
				"1113" : "昌平区",
				"1114" : "平谷区",
				"1115" : "怀柔区",
				"1116" : "门头沟区",
				"1117" : "密云县",
				"1118" : "延庆县"
			},
			"12" :{
				"1201" : "天津",
				"1202" : "河西区",
				"1203" : "河东区",
				"1204" : "河北区",
				"1205" : "南开区",
				"1206" : "和平区",
				"1207" : "红桥区",
				"1208" : "西青区",
				"1209" : "东丽区",
				"1210" : "津南区",
				"1211" : "北辰区",
				"1212" : "滨海新区",
				"1213" : "宝坻区",
				"1214" : "武清区",
				"1215" : "蓟县",
				"1216" : "静海县",
				"1217" : "宁河县"
			},
			"13":{
				"1301" : "保定",
				"1302" : "石家庄",
				"1303" : "唐山",
				"1304" : "秦皇岛",
				"1305" : "邯郸",
				"1306" : "邢台",
				"1307" : "张家口",
				"1308" : "承德",
				"1309" : "沧州",
				"1310" : "廊坊",
				"1311" : "衡水"
			},
			"14":{
				"1401" : "长治",
				"1402" : "大同",
				"1403" : "晋城",
				"1404" : "晋中",
				"1405" : "临汾",
				"1406" : "吕梁",
				"1407" : "朔州",
				"1408" : "太原",
				"1409" : "忻州",
				"1410" : "阳泉",
				"1411" : "运城"
			},
			"15":{
				"1501" : "呼和浩特",
				"1502" : "包头",
				"1503" : "鄂尔多斯",
				"1504" : "乌兰察布",
				"1505" : "准格尔旗",
				"1506" : "锡林郭勒盟",
				"1507" : "通辽",
				"1508" : "巴彦淖尔",
				"1509" : "乌海",
				"1510" : "呼伦贝尔",
				"1511" : "赤峰",
				"1511" : "兴安盟",
				"1511" : "阿拉善盟"
						
			},
			"16":{
				"1601" : "沈阳",
				"1602" : "鞍山",
				"1603" : "本溪",
				"1604" : "朝阳",
				"1605" : "大连",
				"1606" : "丹东",
				"1607" : "抚顺",
				"1608" : "阜新",
				"1609" : "葫芦岛",
				"1610" : "锦州",
				"1611" : "辽阳",
				"1612" : "铁岭",
				"1613" : "营口",
				"1614" : "盘锦",
				"1615" : "绥中"
			},
			"17":{
				"1701" : "白城",
				"1702" : "白山",
				"1703" : "长春",
				"1704" : "辽源",
				"1705" : "四平",
				"1706" : "松原",
				"1707" : "通化",
				"1708" : "延吉",
				"1709" : "吉林"
			},
			"18":{
				"1801" : "哈尔滨",
				"1802" : "齐齐哈尔",
				"1803" : "大庆",
				"1804" : "牡丹江",
				"1805" : "佳木斯",
				"1806" : "双鸭山",
				"1807" : "鸡西",
				"1808" : "绥化",
				"1809" : "黑河",
				"1810" : "伊春",
				"1811" : "鹤岗",
				"1812" : "七台河"
			},
			"19":{
				"1901" : "上海",
				"1902" : "黄浦区",
				"1903" : "卢湾区",
				"1904" : "徐汇区",
				"1905" : "长宁区",
				"1906" : "静安区",
				"1907" : "普陀区",
				"1908" : "闸北区",
				"1909" : "虹口区",
				"1910" : "杨浦区",
				"1911" : "宝山区",
				"1912" : "闵行区",
				"1913" : "嘉定区",
				"1914" : "松江区",
				"1915" : "青浦区",
				"1916" : "奉贤区",
				"1917" : "金山区",
				"1918" : "浦东新区",
				"1919" : "崇明县"
			},
			"20":{
				"2001" : "南京",
				"2002" : "苏州",
				"2003" : "徐州",
				"2004" : "扬州",
				"2005" : "镇江",
				"2006" : "无锡",
				"2007" : "常州",
				"2008" : "南通",
				"2009" : "泰州",
				"2010" : "盐城",
				"2011" : "淮安",
				"2012" : "宿迁",
				"2013" : "连云港"
			},
			"21":{
				"2101" : "杭州",
				"2102" : "宁波",
				"2103" : "湖州",
				"2104" : "金华",
				"2105" : "嘉兴",
				"2106" : "绍兴",
				"2107" : "萧山",
				"2108" : "余杭",
				"2109" : "温州",
				"2110" : "台州",
				"2111" : "丽水",
				"2112" : "舟山"
			},
			"22":{
				"2201" : "蚌埠",
				"2202" : "合肥",
				"2203" : "六安",
				"2204" : "巢湖",
				"2205" : "马鞍山",
				"2206" : "芜湖",
				"2207" : "安庆",
				"2208" : "铜陵",
				"2209" : "池州",
				"2210" : "宣城",
				"2211" : "黄山",
				"2212" : "滁州",
				"2213" : "淮南",
				"2214" : "阜阳",
				"2215" : "宿州",
				"2216" : "淮北",
				"2217" : "毫州"
			},
			"23":{
				"2301" : "漳州",
				"2302" : "福州",
				"2303" : "厦门",
				"2304" : "龙岩",
				"2305" : "南平",
				"2306" : "宁德",
				"2307" : "莆田",
				"2308" : "泉州",
				"2309" : "三明",
			},
			"24":{
				"2401" : "南昌",
				"2402" : "抚州",
				"2403" : "赣州",
				"2404" : "吉安",
				"2405" : "宜春",
				"2406" : "上饶",
				"2407" : "鹰谭",
				"2408" : "九江",
				"2409" : "景德镇",
				"2410" : "萍乡",
				"2411" : "新余"
			},
			"25":{
				"2501" : "济南",
				"2502" : "聊城",
				"2503" : "青岛",
				"2504" : "泰安",
				"2505" : "烟台",
				"2506" : "淄博",
				"2507" : "日照",
				"2508" : "德州",
				"2509" : "济宁",
				"2510" : "威海",
				"2511" : "莱芜",
				"2512" : "潍坊",
				"2513" : "东营",
				"2514" : "菏泽",
				"2515" : "枣庄",
				"2516" : "临沂",
				"2517" : "滨州"
			},
			"26":{
				"2601" : "南阳",
				"2602" : "开封",
				"2603" : "平顶山",
				"2604" : "濮阳",
				"2605" : "三门峡",
				"2606" : "新乡",
				"2607" : "郑州",
				"2608" : "洛阳",
				"2609" : "安阳",
				"2610" : "周口",
				"2611" : "信阳",
				"2612" : "驻马店",
				"2613" : "漯河",
				"2614" : "焦作",
				"2615" : "许昌",
				"2616" : "鹤壁",
				"2617" : "济源",
				"2618" : "商丘"
			},
			"27":{
				"2701" : "武汉",
				"2702" : "鄂州",
				"2703" : "恩施",
				"2704" : "黄冈",
				"2705" : "黄石",
				"2706" : "荆门",
				"2707" : "荆州",
				"2708" : "十堰",
				"2709" : "仙桃",
				"2710" : "咸宁",
				"2711" : "襄阳",
				"2712" : "孝感",
				"2713" : "宜昌",
				"2714" : "神农架",
				"2715" : "随州",
				"2716" : "天门",
				"2717" : "潜江"
			},			
			"28":{
				"2801" : "长沙",
				"2802" : "怀化",
				"2803" : "岳阳",
				"2804" : "株洲",
				"2805" : "湘潭",
				"2806" : "衡阳",
				"2807" : "邵阳",
				"2808" : "郴州",
				"2809" : "常德",
				"2810" : "益阳",
				"2811" : "张家界",
				"2812" : "湘西",
				"2813" : "永州",
				"2814" : "娄底"
			},
			"29":{
				"2901" : "广州",
				"2902" : "阳江",
				"2903" : "潮州",
				"2904" : "东莞",
				"2905" : "佛山",
				"2906" : "河源",
				"2907" : "惠州",
				"2908" : "江门",
				"2909" : "茂名",
				"2910" : "汕头",
				"2911" : "汕尾",
				"2912" : "韶关",
				"2913" : "顺德",
				"2914" : "云浮",
				"2915" : "湛江",
				"2916" : "肇庆",
				"2917" : "中山",
				"2918" : "珠海",
				"2919" : "揭阳",
				"2920" : "梅州",
				"2921" : "清远"
			},
			"30":{
				"3001" : "南宁",
				"3002" : "柳州",
				"3003" : "桂林",
				"3004" : "北海",
				"3005" : "玉林",
				"3006" : "梧州",
				"3007" : "钦州",
				"3008" : "贵港",
				"3009" : "河池",
				"3010" : "百色",
				"3011" : "防城港",
				"3012" : "来宾",
				"3013" : "贺州",
				"3014" : "崇左"
			},
			"31":{
				"3101" : "海口",
				"3102" : "三亚"
			},
			"32":{
				"3201" : "深圳"
			},
			"33":{
				"3301" : "成都",
				"3302" : "资阳",
				"3303" : "凉山",
				"3304" : "德阳",
				"3305" : "绵阳",
				"3306" : "雅安",
				"3307" : "眉山",
				"3308" : "乐山",
				"3309" : "自贡",
				"3310" : "内江",
				"3311" : "宜宾",
				"3312" : "遂宁",
				"3313" : "南充",
				"3314" : "广安",
				"3315" : "达州",
				"3316" : "巴中",
				"3317" : "广元",
				"3318" : "阿坝",
				"3319" : "甘孜",
				"3320" : "攀枝花",
				"3321" : "泸州"
			},
			"34":{
				"3401" : "贵阳",
				"3402" : "六盘水",
				"3403" : "安顺",
				"3404" : "毕节",
				"3405" : "黔东南",
				"3406" : "黔南",
				"3407" : "黔西南",
				"3408" : "铜仁",
				"3409" : "遵义"
			},
			"35":{
				"3501" : "版纳",
				"3502" : "保山",
				"3503" : "楚雄",
				"3504" : "大理",
				"3505" : "德宏",
				"3506" : "迪庆",
				"3507" : "红河",
				"3508" : "昆明",
				"3509" : "丽江",
				"3510" : "临沧",
				"3511" : "怒江",
				"3512" : "曲靖",
				"3513" : "普洱",
				"3514" : "文山",
				"3515" : "玉溪",
				"3516" : "昭通"
			},
			"36":{
				"3601" : "拉萨",
				"3602" : "其他"
			},
			"37":{
				"3701" : "重庆",
				"3702" : "渝中区",
				"3703" : "大渡口区",
				"3704" : "江北区",
				"3705" : "南岸区",
				"3706" : "沙坪坝区",
				"3707" : "九龙坡区",
				"3708" : "北碚区",
				"3709" : "万盛区",
				"3710" : "双桥区",
				"3711" : "渝北区",
				"3712" : "巴南区",
				"3713" : "万州区",
				"3714" : "涪陵区",
				"3715" : "黔江区 ",
				"3716" : "长寿区",
				"3717" : "江津区",
				"3718" : "合川区",
				"3719" : "永川区",
				"3720" : "南川区"
			},
			"38":{
				"3801" : "西安",
				"3802" : "宝鸡",
				"3803" : "铜川",
				"3804" : "延安",
				"3805" : "榆林",
				"3806" : "汉中",
				"3807" : "安康",
				"3808" : "渭南",
				"3809" : "咸阳",
				"3810" : "商洛"
			},
			"39":{
				"3901" : "兰州",
				"3902" : "定西",
				"3903" : "天水",
				"3904" : "平凉",
				"3905" : "庆阳",
				"3906" : "临夏",
				"3907" : "甘南州",
				"3908" : "陇南",
				"3909" : "武威",
				"3910" : "金昌",
				"3911" : "张掖",
				"3912" : "酒泉",
				"3913" : "嘉峪关",
				"3914" : "白银"
			},
			"40":{
				"4001" : "西宁",
				"4002" : "果洛",
				"4003" : "海北",
				"4004" : "海东",
				"4005" : "海南",
				"4006" : "海西",
				"4007" : "黄南",
				"4008" : "玉树"
			},
			"41":{
				"4101" : "银川市",
				"4102" : "吴忠市",
				"4103" : "中卫市",
				"4104" : "石嘴市",
				"4105" : "固原"
			},
			"42":{
				"4201" : "乌鲁木齐",
				"4202" : "博州",
				"4203" : "昌吉",
				"4204" : "哈密",
				"4205" : "和田",
				"4206" : "喀什",
				"4207" : "克拉玛依",
				"4208" : "巴州",
				"4209" : "石河子",
				"4210" : "吐鲁番",
				"4211" : "伊犁",
				"4212" : "阿克苏",
				"4213" : "塔城",
				"4214" : "阿勒泰",
				"4215" : "克州",
				"4216" : "奎屯",
				"4217" : "阿拉尔",
				"4218" : "图木舒克",
				"4219" : "五家渠",
				"4220" : "独山子"
			},
			"43":{
				"4301" : "其他"
			}
	}
	
    var $day = $("#day"),  
        $month = $("#month"),  
        $year = $("#year"),  
        $dDate = new Date(),
        dCurYear = $dDate.getFullYear(),
        str="";
        
    for(var i=dCurYear-52 ; i<dCurYear+1 ; i++)
    {
        if(i==dCurYear){
            str="<option value="+i+" selected=true>"+i+"</option>";
        }else{
            str="<option value="+i+">"+i+"</option>";
        }
        $year.append(str);
    };

    for(var i=1 ; i<=12 ; i++){
        if(i==($dDate.getMonth()+1))
        {
            str="<option value="+i+" selected=true>"+i+"</option>";
        }else{
            str="<option value="+i+">"+i+"</option>";
        }
        $month.append(str);
    };
     
    function GetDaysInMonth(iMonth, iYear) {
        var dPrevDate = new Date(iYear, iMonth, 0);
        return dPrevDate.getDate();
    };

    function UpdateCal(iYear, iMonth , iDay , flag)
    {
        var dDate=new Date();
        daysInMonth = GetDaysInMonth(iMonth, iYear);
        $("#day").empty();
        for (d = 1; d <= parseInt(daysInMonth); d++) {
            if(d==dDate.getDate()){
                str="<option value="+d+" selected=true>"+d+"</option>";
            }else{
                str="<option value="+d+">"+d+"</option>";
            }
            $("#day").append(str);
        }
    };
    
	
    $year.change(function( event , flag ){
        if( flag )
        {
            UpdateCal($("#year").attr("data-value"), $("#month").attr("data-value"));
            $("#year").val($("#year").attr("data-value"));
            $("#month").val($("#month").attr("data-value"));
            $("#day").val($("#day").attr("data-value"));
        }
        else
        {
            UpdateCal($("#year").val( ), $("#month").val( ));
        }
        
    })
    $month.change(function( event , flag ){
        if( flag )
        {
            UpdateCal($("#year").attr("data-value"), $("#month").attr("data-value"));
            $("#year").val($("#year").attr("data-value"));
            $("#month").val($("#month").attr("data-value"));
            $("#day").val($("#day").attr("data-value"));
        }
        else
        {
            UpdateCal($("#year").val( ), $("#month").val( ));
        }
    })
    
	$(".user_city").change(function( event , flag ){
		var id = "";
		var html="<option value='-1'>--请选择--</option>";
		if( flag )
		{
		    id = $(this).attr("data-value");
		    $(this).val( id );
		}
		else
	    {
	        id = $(this).val();
	    }
		for(var i in json[id] )
		{
			html += "<option value='"+i+"'>"+json[id][i]+"</option>";
		}
		$(this).next(".subcity").html(html);
		
	})
	
	$(".subcity").change(function( event , flag ){
	    if( flag )
	    {
	        $(this).val( $(this).attr("data-value") );
	    }
	})
	$("#job_category").change(function(event , flag){
	    if( flag )
	    {
	        $(this).val( $(this).attr("data-value") );
	    }
	})
	
	$('#categoryModal').modal( 
	{
		keyboard: true,
		show:false,
		"backdrop"  : "static"
	});
	
	$('#myTab a').click(function (e) {
	    e.preventDefault();
	    $(this).tab('show');
	})
	$('#myTab a:first').tab('show');
	
	$("#ainfo button[type='submit']").click(function(e){
		e.preventDefault();
		$("#notify-wrapper").hide();
		$("#mask").show();
		$(this).parents("form").ajaxSubmit({
			url:"/account/updateUser.json",
			type:"post",
			success : function(){
				$("#notify").addClass("success");
				$("#notify-msg").html("保存成功");
				$("#mask").hide();
				$("#notify-wrapper").fadeIn(1).delay(1000).fadeOut(500 );
			}
		});
	});
	
	$("#industry_str").focus(function(){
		$("#categoryModal").modal("show");
	});
	$("#categoryBtn").click(function(e)
	{
		var str="";
		e.preventDefault();
		$("#categoryModal input:checkbox[checked]").each(function(){
			str += $(this).next().html()+","
		})
		$("#industry_str").val(str.substring(0 , str.length-1));
		$("#categoryModal").modal("hide");
	})
	$("#binfo button[type='submit']").click(function(e){
		e.preventDefault();
		$("#notify-wrapper").hide();
		$("#mask").show();
		$(this).parents("form").ajaxSubmit({
			url:"/account/updateUser.json",
			type:"post",
			success : function(){
				$("#notify").addClass("success");
				$("#notify-msg").html("设置修改成功");
				$("#mask").hide();
				$("#notify-wrapper").fadeIn(1).delay(1000).fadeOut(500 );
			}
		});
	})
	
	$("#cinfo input").change(function( ){
		$(this).parents(".control-group").removeClass("error");
	})
	
	$("#cinfo button[type='submit']").click(function(e){
		e.preventDefault();
		if( $("#npassword").val() != $("#cpassword").val() ){
			$("#cpassword").parents(".control-group").addClass("error");
			$("#resetpwd_error").html("<strong style='color:#B94A48;padding-left:10px'>两次密码不一致</strong>");
		}
		else if( !$("#npassword").val() || !$("#cpassword").val() || !$("#opassword").val())
		{
			if( !$("#opassword").val() )
			{
				$("#opassword").parents(".control-group").addClass("error");
			}
			
			if( !$("#npassword").val() )
			{
				$("#npassword").parents(".control-group").addClass("error");
			}
			
			if( !$("#cpassword").val() )
			{
				$("#cpassword").parents(".control-group").addClass("error");
			}
		}
		else
		{
			$("#notify-wrapper").hide();
			$("#mask").show();
			$(this).parents("form").ajaxSubmit({
				url:"/account/resetPassword.json",
				type:"post",
				success : function(data){
					if(eval(data.result))
					{
						$("#notify").addClass("success");
						$("#notify-msg").html("设置修改成功");
						$("#mask").hide();
						$("#notify-wrapper").fadeIn(1).delay(1000).fadeOut(500 );
					}
					else
					{
						$("#notify").addClass("fail");
						$("#notify-msg").html("旧密码错误");
						$("#mask").hide();
						$("#notify-wrapper").fadeIn(1).delay(1000).fadeOut(500 );
					}
					
				}
			});
		}
	})
	
	$("#dinfo button[type='submit']").click(function(e){
		e.preventDefault();
		$("#notify-wrapper").hide();
		$("#mask").show();
		$(this).parents("form").ajaxSubmit({
			url:"/account/updateUser.json",
			type:"post",
			success : function(){
				$("#notify").addClass("success");
				$("#notify-msg").html("设置修改成功");
				$("#mask").hide();
				$("#notify-wrapper").fadeIn(1).delay(1000).fadeOut(500 );
			}
		});
	})
	
	
	$("#info .cancel").click(function(e){
		e.preventDefault();
	})
	
	if( !eval($("#male").attr("data-value")) )
	{
	     $("#male").attr("checked","checked");
	}
	else
	{
	    $("#fmale").attr("checked","checked");
	}
	$("#myspace .unauth").click( function(e){
	    var target = e.target;
	    $.ajax({
	        url : "/account/cancelAuthorization.json",
	        type:"post",
	        data : {id:$(target).attr("data-id")},
	        success : function(data){
	            if(data.result == "success")
	            {
	                $(target).parents(".auth_content").remove();
	            }
	        }
	    })
	})
	
	$("#add_city").trigger('change' ,true);
	$("#add_scity").trigger('change' , true );
	$("#com_address_city").trigger('change' , true);
	$("#com_address_scity").trigger('change',true );
	$("#job_category").trigger('change',true );
	$("#year").trigger('change',true );
	$("#month").trigger('change',true );
	
	$('#glodon_error_dlg').modal( 
    {
    	keyboard: true,
    	show:false,
    	"backdrop"  : "static"
    });
        	
	$.ajaxSetup({
	    global:false,
	    error : function( XMLHttpRequest, textStatus, errorThrown ){
	        if( XMLHttpRequest.responseText == "logout" )
	        {
	            window.location.replace( window.location.href);    
	        }
	        else
	        {
	            $('#glodon_error_dlg').modal("show");
    	        $('#glodon_error_dlg .modal-body').html( XMLHttpRequest.responseText );
    	        $('#mask').hide();
	        }
	    }
	}) 
})
